package com.zhiyou.one;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.ScrollView;

public class MainActivity extends Activity {

    //右上角分享按钮

    Button button;

    //底部选项卡按钮
    RadioGroup group;

    //内容显示区域的滚动视图
    ScrollView scrollView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button =(Button)findViewById(R.id.action_bar_more);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                initPopWindow();

            }
        });


        //获取到滚动视图，并设置首页界面的内容
        scrollView = (ScrollView) findViewById(R.id.m_scrollView);
        setContentLayout(R.layout.home_layout);

        //获取底部选项卡按钮，并进行行事件监听，从而设置对应的界面内容
        group = (RadioGroup) findViewById(R.id.radio_group);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.btn_home:
                        setContentLayout(R.layout.home_layout);
                        break;
                    case R.id.btn_article:
                        setContentLayout(R.layout.article_layout);
                        break;
                    case R.id.btn_question:
                        setContentLayout(R.layout.question_layout);
                        break;
                    case R.id.btn_things:
                        setContentLayout(R.layout.things_layout);
                        break;
                    case R.id.btn_person:
                        setContentLayout(R.layout.person_layout);
                        break;
                }
            }
        });

    }
    //设置界面内容的方法
    public void setContentLayout(int id){
        scrollView.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View view = inflater.inflate(id,null);
        scrollView.addView(view);
    }

    //设置弹出视图

    private void initPopWindow(){

        backgroundAlpha(0.5f);

        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popwindow_layout,null);
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        popWindow.setBackgroundDrawable(new ColorDrawable(0xffffffff));
        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });

        popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0);

    }

    /**
     * 设置添加屏幕的背景透明度
     */
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

}
